<?php
/** [文档表服务层模型]
 * @Author: 976123967@qq.com
 * @Date:   2015-01-16 15:44:28
 * @Last Modified by:   Administrator
 * @Last Modified time: 2015-03-16 09:13:32
 */
class ArticleService extends Model{



	/**
	 * [get_flag 读取文档信息没有分页 artlist标签]
	 * @param  [type] $cid   [description]
	 * @param  [type] $flag  [description]
	 * @param  [type] $row   [description]
	 * @param  [type] $order [description]
	 * @return [type]        [description]
	 */
	public function get_flag($cid,$flag,$row,$order)
	{

		/***扩展表名称***/
		$categoryAll = S('category');
		$category    = $categoryAll[$cid];
		$mid         = $category['model_mid'];
		// 读取表名称
		$modelAll    = S('model');
		$table 		 = 'article_'.$modelAll[$mid]['name'];

		/***扩展字段***/
		// 显示在列表的扩展字段
		$fieldService= D('Field');
		$extField    = $fieldService->get_lists_field($mid);
		$imgField 	 = $fieldService->get_img_field($mid);


		// 审核通过
		$where['verifystate'] = 2;
		$categoryLogic 		  = D('Category');
		if($cid)
		{
			// 获取所有的子cid
			$childCid 			   = $categoryLogic->get_child_cid($cid);
			$where['category_cid'] = array('in',$childCid) ;
		}

		if($flag)
		{
			// 解析成正确的值
			$infoFlag 	 = C('flag');

			$flag 		 = isset($infoFlag[$flag])?$infoFlag[$flag]:'';
			if($flag)
				$where[] = "find_in_set('{$flag}',flag)";			
		}


		// 排序
		if(!$order)
			$order = 'article.sort asc ,aid desc';

		
		$field = 'sort,verifystate,aid,category_cid,article_title,keywords,description,username,cname,remark,edittime,addtime,pic,flag,article_title_en,keywords_en,description_en';
		
		$data = D("FlagView")->where($where)->order($order)->limit($row)->select();;
		
		if(!$data) return $data;
		$db =  M($table);
		foreach($data as $k=>$v)
		{
			$data[$k]['time'] 	 = $v['addtime'];
			// 发布时间
			$data[$k]['addtime'] = date('Y-m-d',$v['addtime']);
			// 图片
			$data[$k]['pic']	 = $v['pic']?__ROOT__.'/'.$v['pic']:__ROOT__.'/Data/Public/images/default.gif';

			// 扩展字段
			if($extField)
			{
				$temp = $db->field($extField)->where(array('article_aid'=>$v['aid']))->find();
				
				$extArr = explode(',', $extField);
				// 图片字段路径修改
				$imgArr = explode(',', $imgField);
				foreach($extArr as $ext)
				{
					if($imgField && in_array($ext, $imgArr))
					{
						$temp[$ext] = $temp[$ext]?__ROOT__.'/'.$temp[$ext]:__ROOT__.'/Data/Public/images/default.gif';
					}
				}	
					
				


				$data[$k] = array_merge($data[$k],$temp);
			}

			$remark 			 = strtolower($v['remark']);
			$data[$k]['url']     =U('/'.$remark.'_v_'.$v['category_cid'].'_'.$v['aid']);
			$data[$k]['murl']    =U(MOBILE_NAME.'/'.$v['remark'].'/view',array('category_cid'=>$v['category_cid'],'aid'=>$v["aid"]));
			// 下载
			$data[$k]['downurl'] = $v['file']?U('down',array('aid'=>$v['aid'],'field'=>'file')):'';

			
		}

	
		return $data;
	}



	/**
	 * [find_aid_by_category_cid 通过cid找aid 单页面用]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function find_aid_by_category_cid($cid)
	{
		$data = D('Article')->where(array('category_cid'=>$cid))->find();
		if(!$data)
			return false;
		return $data['aid'];
	}


	/**
	 * [find_one_by_table_aid 查找详细]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function find_one_by_table_aid($data)
	{
		$data = D('ArticleView')->find_one_by_aid($data);
		if(!$data) return false;
		$data['time']    = $data['addtime'];
		$data['addtime'] = date('Y-m-d',$data['addtime']);
		// 图片地址
		$data['pic']     = $data['pic']?__ROOT__.'/'.$data['pic']:__ROOT__.'/Data/Public/images/default.gif';


		// 自定义图片字段图片路径
		$categoryAll = S('category');
		$category    = $categoryAll[$data['category_cid']];
		$mid         = $category['model_mid'];
		$imgField 	 = D('Field')->get_img_field($mid);

		if($imgField)
		{
			$imgArr = explode(',', $imgField);
		
			foreach($imgArr as $ext)
			{
				
				$data[$ext] = $data[$ext]?__ROOT__.'/'.$data[$ext]:__ROOT__.'/Data/Public/images/default.gif';
			}	
	
		}
		
		return $data;
	}



	/**
	 * [get_bread 获取面包屑导航]
	 * @return [type] [description]
	 */
	public function get_bread($cid,$aid=0)
	{
		if($aid)
		{
			$articleTitle = D('Article')->where(array('aid'=>$aid))->getField('article_title');
			//$articleTitleEn = D('Article')->where(array('aid'=>$aid))->getField('article_title_en');
		}

		$categoryLogic = D('Category');

		// 所有父级
		$parent = $categoryLogic->get_parent_list($cid);
		$parent = array_reverse($parent);
		$bread  = "<a href='".__ROOT__."/'>首页</a> &gt; ";
		//$breadEn ="<a href='".__ROOT__."/'>Home</a> &gt; ";
		foreach ($parent as $k=> $v) 
		{
			
			$url = $categoryLogic->get_url($v);
			if($k<count($parent)-1)
			{
				$bread .= "<a href='".$url."'>".$v['cname'].'</a> &gt; ';
				//$breadEn .= "<a href='".$url."'>".$v['cname_en'].'</a> &gt; ';
			}
			elseif($aid)
			{
				$bread .= "<a href='".$url."'>".$v['cname'].'</a> &gt; '.$articleTitle;
				//$breadEn .= "<a href='".$url."'>".$v['cname_en'].'</a> &gt; '.$articleTitleEn;
			
			}
			else
			{
				$bread.="".$v['cname']."";
				//$breadEn.="".$v['cname_en']."";
			}
		}
		//return array('bread'=>$bread,'bread_en'=>$breadEn);
		return array('bread'=>$bread);
	}


	/**
	 * [get_title 页面标题]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function get_title($cid,$aid=0)
	{
		
		// 所有父级
		$parent = D('Category')->get_parent_list($cid);

		$title  = "";
		//$titleEn = "";
		foreach ($parent as $k=> $v) 
		{

			if($k<count($parent)-1)
			{
				$title .= "{$v['cname']} > ";
				//$titleEn .= "{$v['cname_en']} > ";
			}
			elseif($aid)
			{

				$articleTitle = D('Article')->where(array('aid'=>$aid))->getField('article_title');
				//$articleTitleEn = D('Article')->where(array('aid'=>$aid))->getField('article_title_en');
				$title = $articleTitle." > ".$title;
				// $titleEn = $articleTitleEn." > ".$titleEn;
			}
			else
			{
				$title .= "{$v['cname']} > ";
				//$titleEn .= "{$v['cname_en']} > ";
			}
		
		}
		$title .= C('cfg_name');
		//$titleEn .= C('cfg_name_en');

		//return array('page_title'=>$title,'page_title_en'=>$titleEn);
		return array('page_title'=>$title);
	}



	/**
	 * [get_list 列表 pagelist标签]
	 * @param  [type] $order [description]
	 * @return [type]        [description]
	 */
	public function get_list($order,$lang='cn')
	{
		// 关键词
		$searchKeyword  = I('get.search_keyword','');
		// 搜索关键词
		if($searchKeyword )
		{
			$where[] = "article_title like '%{$searchKeyword}%'";
			
		}


		// 分类ID
		$cid = I('get.cid');
		$childCids = D('Category')->get_child_cid($cid);
		$where['category_cid'] = array('in',$childCids);

		$where['verifystate'] = 2;



		/***扩展表名称***/
		$categoryAll = S('category');
		$category    = $categoryAll[$cid];
		$mid         = $category['model_mid'];
		// 读取表名称
		$modelAll    = S('model');
		$table 		 = 'article_'.$modelAll[$mid]['name'];

		/***扩展字段***/
		// 显示在列表的扩展字段
		$fieldService= D('Field');
		$extField    = $fieldService->get_lists_field($mid);
		$imgField 	 = $fieldService->get_img_field($mid);

		$articleViewModel = D('ArticleView');

		
		$count = $this->where($where)->count();
	
		// 没有数据
		if(!$count)
			return false;
		// 分页
		import('ORG.Util.Page');// 导入分页类

		$page  = new Page($count,$category['page']);
		
		$field ='sort,verifystate,aid,category_cid,article_title,keywords,description,username,cname,remark,edittime,addtime,pic,file,flag,article_title_en,keywords_en,description_en';

		if($extField)
			$field .= ','.$extField;
		

		
		// 排序
		if(!$order)
			$order = 'article.sort asc ,aid desc';
		// limit
		$limit = $page->firstRow.','.$page->listRows;
		// 读取数据
		$data  = $articleViewModel->find_list($where,$order,$limit,$field,$table);
		//echo $articleViewModel->_sql();die;
		// 处理数据
		foreach ($data as $k=>$v)
		{

			
			// 编辑时间
			$data[$k]['edittime']    = date('Y-m-d',$v['edittime']);
			// 发布时间
			$data[$k]['time']        = $v['addtime'];
			$data[$k]['addtime']     = date('Y-m-d',$v['addtime']);

			// 控制器
			$remark 			 = strtolower($v['remark']);
			// 当前URL地址
			$data[$k]['url']     = U('/'.$remark.'_v_'.$v['category_cid'].'_'.$v['aid']);
			$data[$k]['cat_url'] = U('/'.$remark.'_l_'.$v['category_cid']);
			
			$data[$k]['murl']    = U(MOBILE_NAME.'/'.$v['remark'].'/view',array('category_cid'=>$v['category_cid'],'aid'=>$v["aid"]));
			$data[$k]['cat_murl']= U(MOBILE_NAME.'/'.$v['remark'].'/list',array('category_cid'=>$v['category_cid']));
			// 图片地址
			$data[$k]['pic']         = $v['pic']?__ROOT__.'/'.$v['pic']:__ROOT__.'/Data/Public/images/default.gif';

			// 下载
			$data[$k]['downurl']   = $v['file']?U('down',array('aid'=>$v['aid'],'field'=>'file')):'';



			// 自定义图片字段图片路径
			if($extField)
			{
				$extArr = explode(',', $extField);
				$imgArr = explode(',', $imgField);
				foreach($extArr as $ext)
				{
					if($imgField && in_array($ext, $imgArr))
					{
						$data[$k][$ext] = $v[$ext]?__ROOT__.'/'.$v[$ext]:__ROOT__.'/Data/Public/images/default.gif';
					}
				}	
			}
			


		}
		if($lang == 'en')
		{
			$page->setConfig('header',' record ');
			$page->setConfig('prev',' prev ');
			$page->setConfig('next',' next ');
			$page->setConfig('first',' first ');
			$page->setConfig('last',' last ');
			$page->setConfig('theme','<span>%totalRow% %header% %nowPage%/%totalPage% page </span>%upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%');
		}
		else
		{
			// 分页样式更改
			$page->setConfig('theme','<span>%totalRow% %header% %nowPage%/%totalPage% 页 </span>%upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%');
		}
				
	

		// 页脚信息
		$data['page'] = $page->show();


		return $data;
	
	}



	/**
	 * [next 上一条和下一条]
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function next($flag=1,$lang='cn')
	{

		$aid =  I('get.aid',null,'intval');
		$cid = I('get.cid',null,'intval');

		$where = 'aid<'.$aid.' and category_cid='.$cid;
		$order ='aid desc';
		$field = 'remark,cid,aid,article_title,category_cid,article_title_en';
		
		// 下一条
		$fieldNext = D('FlagView')->find_one($where,$order,$field);
		if($fieldNext)
		{
			// 控制器
			$remark = strtolower($fieldNext['remark']);
			// 当前URL地址
			$url  =U('/'.$remark.'_v_'.$fieldNext['category_cid'].'_'.$fieldNext['aid']);
			$murl  = U(MOBILE_NAME.'/'.$remark.'/vew',array('category_cid'=>$fieldNext['category_cid'],'aid'=>$fieldNext['aid']));
			if($lang=='cn')
			{
				$next = "<a href='".$url."'>{$fieldNext['article_title']}</a>";
				$mnext = "<a href='".$murl."'>{$fieldNext['article_title']}</a>";
			}
			else
			{
				$next = "<a href='".$url."'>{$fieldNext['article_title_en']}</a>";
				$mnext = "<a href='".$murl."'>{$fieldNext['article_title_en']}</a>";
			}
		}	
		else
		{
			if($lang=='cn')
				$next = '没有了';
			else
				$next = 'none';
		}

		
		// 上一条
		$where = 'aid>'.$aid.' and category_cid='.$cid;
		$order ='aid asc';
		$fieldPre = D('FlagView')->find_one($where,$order,$field);

		if($fieldPre)
		{
			// 控制器
			$remark = strtolower($fieldPre['remark']);
			// 当前URL地址
			$url  = U('/'.$remark.'_v_'.$fieldPre['category_cid'].'_'.$fieldPre['aid']);
			$murl  = U(MOBILE_NAME.'/'.$remark.'/vew',array('category_cid'=>$fieldPre['category_cid'],'aid'=>$fieldPre['aid']));
			if($lang=='cn')
			{
				$pre  = "<a href='".$url."'>{$fieldPre['article_title']}</a>";
				$mpre = "<a href='".$murl."'>{$fieldPre['article_title']}</a>";
			}
			else
			{
				$pre  = "<a href='".$url."'>{$fieldPre['article_title_en']}</a>";
				$mpre = "<a href='".$mpre."'>{$fieldPre['article_title_en']}</a>";
				
			}
		}
		else
			if($lang=='cn')
				$pre = '没有了';
			else
				$pre = 'none';

		switch ($flag) 
		{
			// 上一条和下一条
			case 1:
				return $pre.$next;
			
			// 上一条
			case 2:
				return $pre;
			
			//下一条
			case 3:
				return $next;
				
				
			// 上一条和下一条
			case 4:
				return $mpre.$mnext;
			
			// 上一条
			case 5:
				return $mpre;
			
			//下一条
			case 6:
				return $mnext;	
		}


	}

}